package com.example.demoweb.util.file;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;

@Component
@SuppressWarnings("restriction")
public class Base64Convert {
	BASE64Encoder encoder = new BASE64Encoder();
    public String fileToBase64(InputStream in) throws IOException {
    	String strBase64 = "";
        try {
        	byte[] bytes = IOUtils.toByteArray(in);
            // 将文件中的内容读入到数组中
        	//in.read(bytes);
            //将字节流数组转换为字符串
        	strBase64 = encoder.encode(bytes);     
            in.close();
        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return strBase64;
    }
}